package com.itheima.test31;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;

/*
二．编程题【集合加强】
	3.1 ArrayList有以下元素: "a","f","b","c","a","d"，请用任意方式完成集合元素去重。去除集合中重复的元素。
 */
public class Test31 {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("a");
        arrayList.add("f");
        arrayList.add("b");
        arrayList.add("c");
        arrayList.add("a");
        arrayList.add("d");

        ArrayList arrayList1 = deleteRepeat(arrayList);//调用自定义去重函数
        System.out.println(arrayList1);
        String s = "";

    }

    //自定义去重函数
    private static <T> ArrayList<T> deleteRepeat(ArrayList<T> arrayList) {
        TreeSet<T> ts = new TreeSet<T>();

        for (T t : arrayList) {
            ts.add(t);
        }
        arrayList.clear();
        for (T t : ts) {
            arrayList.add(t);
        }
        return arrayList;
    }
}
